<?php

namespace App\Http\Controllers\Chat;

use App\Http\Controllers\Controller;
use App\Models\Chat\Friend;
use App\Models\Chat\Group;
use App\Models\Chat\GroupUser;
use App\Models\Chat\SystemMessage;
use App\Models\Member\Member;
use Illuminate\Http\Request;

class PublicController extends Controller {
	//chat界面
	public function layimMobile() {
		return view( 'layim.mobile' );
	}
	public function layim() {
		return view( 'layim.index' );
	}

	//绑定 Websocket 客户端ID
	public function bindClientId() {
		$user = auth( 'admin' )->user();
		if ( is_null( $user ) ) {
			return response()->json( [ 'msg' => 'error', 'code' => 400 ], 200 );
		}
		$client_id = \request( 'client_id' );
		//更新用户表 clientId
		$res = Member::where( 'username', $user->username )->update( [
			'client_id'     => $client_id,
			'client_status' => 'online'
		] );

		if ( $res ) {
			//更新群组表 clientId
			GroupUser::where( 'user_id', $user->id )->update( [ 'client_id' => $client_id ] );
			$nick_name = $user->nick_name;
			if ( $nick_name == null or $nick_name == '' ) {
				$nick_name = $user->username;
			}

			return response()->json( [
				'msg'  => 'success',
				'code' => 0,
				'data' => [ 'uid' => $user->id, 'nick_name' => $nick_name ]
			], 200 );
		} else {
			return response()->json( [ 'msg' => 'error', 'code' => 400 ], 400 );
		}

	}

	//关闭客户端
	public function closeClient() {

	}

	//获取 好友群组列表
	public function getList() {
		$user = auth( 'admin' )->user();
		if ( is_null( $user ) ) {
			return response()->json( [ 'msg' => '用户未登录', 'code' => 400 ], 200 );
		}

		//构造好友信息
		$friend         = $user->friend()->pluck( 'friend_id' );
		$friend_info    = Member::wherein( 'id', $friend )->get();
		$friend_data [] = [
			'groupname' => '我的好友',
			'id'        => '1',
		];
		foreach ( $friend_info as $item ) {
			$friend_data[0]['list'][] = [
				'username' => $item->nick_name,
				'id'       => $item->id,
				'avatar'   => ! empty( $item->avatar ) ? asset( 'storage/' . $item->avatar ) : asset( 'storage/image/no_image.jpg' ),
				//'avatar'   => asset( 'storage/' . $item->avatar ),
				'sign'     => $item->sigin,
				'status'   => $item->client_status,
			];
		}
		//构造群信息
		$group      = $user->group()->pluck( 'group_id' );
		$group_info = Group::wherein( 'id', $group )->get();
		$group_data = [];
		foreach ( $group_info as $item ) {
			$group_data[] = [
				'groupname' => $item->group_name,
				'id'        => $item->id,
				'avatar'    => ! empty( $item->group_avatar ) ? asset( 'storage/' . $item->group_avatar ) : asset( 'storage/image/no_image.jpg' ),
				//'avatar'   => asset( 'storage/' . $item->group_avatar ),
			];
		}
		//	dd($group_data);


		$data = [
			'code' => 0,
			'msg'  => 'success',
			'data' => [
				'mine'   =>
					[
						'username' => $user->nick_name,
						'id'       => $user->id,
						'status'   => 'online',
						'sign'     => $user->id,
						'avatar'   => asset( 'storage/' . $user->avatar ),
					],
				'friend' => $friend_data,
				'group'  => $group_data,
			]
		];

		//	dd( $data );

		return response()->json( $data );

	}

	//搜索 好友&群组
	public function search() {
		$user         = auth( 'admin' )->user();
		$search_user  = Member::where( 'username', \request( 'query' ) )->where( 'id', '<>', $user->id )->first();
		$search_group = Group::where( 'id', \request( 'query' ) )->first();
		$group_user   = GroupUser::where( 'group_id', \request( 'query' ) )->where( 'user_id', $user->id )->first();
		$data         = [];
		if ( ! is_null( $group_user ) ) {
			return response()->json( [ 'msg' => 'success', 'code' => 0, 'data' => $data ], 200 );
		}
		if ( ! is_null( $search_user ) ) {
			$data['friend']['id']        = $search_user->id;
			$data['friend']['username']  = $search_user->username;
			$data['friend']['nick_name'] = $search_user->nick_name;
			$data['friend']['avatar']    = ! empty( $search_user->avatar ) ? asset( 'storage/' . $search_user->avatar ) : asset( 'storage/image/no_image.jpg' );
		}
		if ( ! is_null( $search_group ) ) {
			$data['group']['id']         = $search_group->id;
			$data['group']['group_name'] = $search_group->group_name;
			$data['group']['avatar']     = ! empty( $search_group->avatar ) ? asset( 'storage/' . $search_group->avatar ) : asset( 'storage/image/no_image.jpg' );
		}

		return response()->json( [ 'msg' => 'success', 'code' => 0, 'data' => $data ], 200 );
	}

	//添加 好友&群组
	public function add() {

		$user = auth( 'admin' )->user();
		if ( $user->id == \request()->get( 'id' ) ) {
			return response()->json( [ 'msg' => '不能添加自己', 'code' => 400 ], 400 );
		}
		$data ['user_id']   = $user->id;
		$data ['target_id'] = \request()->get( 'id' );
		$data ['message']   = '';
		if ( \request()->get( 'type' ) == 'friend' ) {
			$data ['type'] = 1;
		} else {
			$data ['type'] = 2;
		}
		$res = SystemMessage::create( $data );
		if ( $res ) {
			return response()->json( [ 'msg' => 'success', 'code' => 0 ], 200 );
		} else {
			return response()->json( [ 'msg' => 'error', 'code' => 400 ], 400 );
		}
	}

	//添加列表 好友&群组
	public function addList() {

		$user     = auth( 'admin' )->user();
		$my_group = Group::where( 'group_admin_id', $user->id )->pluck( 'id' );

		$friend_add = SystemMessage::where( 'target_id', $user->id )->where( 'type', '1' )->where( 'action',
			false )->get();
		$group_add  = SystemMessage::wherein( 'target_id', $my_group )->where( 'type', '2' )->where( 'action',
			false )->get();
		$data       = [];
		foreach ( $friend_add as $item ) {
			$temp   = $item->user()->first();
			$data[] = [
				'id'         => $item->id,
				'nick_name'  => $temp->nick_name,
				'avatar'     => ! empty( $temp->avatar ) ? asset( 'storage/' . $temp->avatar ) : asset( 'storage/image/no_image.jpg' ),
				'message'    => $item->message,
				'type'       => $item->type,
				'created_at' => $item->created_at,
			];
		}
		foreach ( $group_add as $item ) {
			$temp       = $item->user()->first();
			$group_temp = $item->group()->first();
			$data[]     = [
				'id'         => $item->id,
				'nick_name'  => $temp->nick_name,
				'group_name' => $group_temp->group_name,
				'avatar'     => ! empty( $temp->avatar ) ? asset( 'storage/' . $temp->avatar ) : asset( 'storage/image/no_image.jpg' ),
				'message'    => $item->message,
				'type'       => $item->type,
				'created_at' => $item->created_at,
			];
		}

		return response()->json( [ 'msg' => 'success', 'code' => 0, 'data' => $data ], 200 );
	}

	//同意请求
	public function addAssent() {

		$user            = auth( 'admin' )->user();
		$system_msg_info = SystemMessage::where( 'id', \request()->get( 'id' ) )->where( 'action', false )->first();
		$res             = null;

		if ( $system_msg_info->type == 1 ) {
			//添加好友 到关系表
			$data = [ 'user_id' => $user->id, 'friend_id' => $system_msg_info->user_id ];
			$res  = Friend::firstOrCreate( $data );

			//标记申请请求

		} elseif ( $system_msg_info->type == 2 ) {
			//判断是否有管理权限
			$group_admin = Group::where( 'group_admin_id', $user->id )->where( 'id',
				$system_msg_info->target_id )->first();
			if ( is_null( $group_admin ) ) {
				return response()->json( [ 'msg' => '无权限', 'code' => 400 ], 400 );
			}

			//加入到 群用户表
			$data = [ 'user_id' => $system_msg_info->user_id, 'group_id' => $system_msg_info->target_id ];
			$res  = GroupUser::firstOrCreate( $data );
			//标记申请请求
		}

		if ( $res ) {
			$system_msg_info->action = true;
			$system_msg_info->save();

			return response()->json( [ 'msg' => 'success', 'code' => 0 ], 200 );
		} else {
			return response()->json( [ 'msg' => '请求失败', 'code' => 400 ], 400 );
		}
	}

	//上传图片
	public function uploadImg() {
		dd( 1 );
	}

	//上传文件
	public function uploadFile() {
		dd( 1 );
	}

}

